|
|
|
|
|
|
|
Figure 21.6
Whereas Print# gives you more control over output format,
Write# adds delimiters for easy retrieval of information. |
|
|
|
|
|
|
|
|
Random FilesCreating Your Own File Format |
|
|
|
|
|
|
|
|
Sequential files do not have any structure. As a matter of fact, the structure is defined by the code that reads the file rather than in the file itself. As you learned in the previous section, this feature is advantageous from a readability standpoint but limits the functions that a programmer can use to search through the file. For example, Visual Basic does not contain a function to jump to a specific record in a sequential file because Visual Basic does not know the file's structure. One way to move some structure into the file itself is to store user-defined record types rather than just string data. You can then open them in Random mode and are not limited to sequential access. |
|
|
|
|
|
|
|
|
Custom record types are really just user-defined data types. You create user-defined data types by using the Type statement. Type declarations are entered in the general declarations section of a Code window. The following code uses the Type statement to declare an Employee record type: |
|
|
|
|
|
|
|
|
Private Type Employee
EmpID As Integer
LName As String * 30
Fname As String * 20
Title As String * 20
End Type |
|
|
|
|
|
|
|
|
The custom record type can then be accessed from code by using dot notation, as in the following example: |
|
|
|
|
|
|
|
|
Dim emp1 As Employee
Emp1.Fname= "Joe"
Emp1.Lname= "Smith"
Emp1.Title= "Chicken Plucker"
Emp1.EmpID = 12345 |
|
|
|
|
|
|
|
|
Notice that accessing fields in a custom data type is similar to accessing an object's properties, as discussed in Chapter 4, "Using Visual Basic's Default Controls." |
|
|
|
|
|